import request from "@/utils/request";

// 属性值
export interface ResAttrValue {
  // 值的id
  id: number;
  // 值所属的属性的id
  attrId: number;
  // 属性值的文本
  valueName: string;
}

// 属性
export interface ResAttr {
  // 属性id
  id: number;
  // 属性的名称
  attrName: string;
  // 属性所属的分类id
  categoryId: number;
  // 属性所属的分类的级别
  categoryLevel: number;
  // 属性值的列表集合
  attrValueList: ResAttrValue[];
}

// 保存接口需要的数据结构
export type ReqSaveAttrValue = Pick<ResAttrValue, "valueName"> & {
  id?: number;
  attrId?: number;
} & {
  isEdit?: boolean;
};
export type ReqSaveAttr = Omit<ResAttr, "id" | "attrValueList"> & {
  id?: number;
} & {
  attrValueList: ReqSaveAttrValue[];
};

enum URLS {
  ATTR_INFO_LIST = "/product/attrInfoList",
  SAVE = "/product/saveAttrInfo",
  DELEtE = "/product/deleteAttr",
}

// 获取平台属性列表
export function requestAttrInfoList(
  category1Id: number,
  category2Id: number,
  category3Id: number
) {
  return request.get<any, ResAttr[]>(
    `${URLS.ATTR_INFO_LIST}/${category1Id}/${category2Id}/${category3Id}`
  );
}

// 提交保存平台属性
export function requestSaveAttrInfo(data: ReqSaveAttr) {
  return request.post<any, any>(URLS.SAVE, data);
}

// 删除平台属性
export function requestDeleteAttrInfo(id: number) {
  return request.delete<any, any>(`${URLS.DELEtE}/${id}`);
}
